import rclpy
from rclpy.node import Node
from rclpy.action import ActionClient
from base_interface_demo01.action import Progress
from rclpy.task import Future
from rclpy.action.client import ClientGoalHandle

class ProgressActionClientNode(Node):
    def __init__(self):
        super().__init__('progress_action_client_node_py')
        self.get_logger().info('ProgressActionClientNode创建')
        self.client_:ActionClient=ActionClient(self,Progress,"get_sum",)
    def send_goal(self,num:int):
        self.client_.wait_for_server()
        goal=Progress.Goal()
        goal.num=num
        future:Future=self.client_.send_goal_async(goal,self.feedback_callback)
        future.add_done_callback(self.goal_resp_done_callback)
        pass
    def feedback_callback(self,fb_msg:Progress.Feedback):
        progress=fb_msg.feedback.progress
        self.get_logger().info('连续反馈数据%.2f'%progress)
    def goal_resp_done_callback(self,future:Future):
        # 这个是目标值的处理 
        goal_handler:ClientGoalHandle=future.result()
        if not goal_handler.accepted:
            self.get_logger().info('目标被拒绝')
            return
        self.get_logger().info('目标被接收正在处理中')
        result_future:Future=goal_handler.get_result_async()
        result_future.add_done_callback(self.get_done_callback)
    def get_done_callback(self,future:Future):
        goal_handler:ClientGoalHandle=future.result()
        # self.get_logger().info(goal_handler.__str__())
        result=goal_handler.result
        self.get_logger().info('最终结果：%d'%result.sum)
        pass
        
def main():
    import sys
    from rclpy.logging import  get_logger
    if len(sys.argv)!=2:
        get_logger("rclpy").error("请提交一个整形数据")
        return 1
    rclpy.init()
    client_node=ProgressActionClientNode()
    num=int(sys.argv[1])
    client_node.send_goal(num)
    rclpy.spin(client_node)
    rclpy.shutdown()


if __name__ == '__main__':
    main()